using System.Security.Claims;
using Microsoft.AspNetCore.Http;

namespace Infrastructure
{
    /// <summary>
    /// 当前用户服务实现类，通过HttpContext获取当前用户信息
    /// </summary>
    public class CurrentUserService : ICurrentUserService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="httpContextAccessor">HTTP上下文访问器</param>
        public CurrentUserService(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 获取当前用户ID
        /// </summary>
        public string UserId => _httpContextAccessor.HttpContext?.User?.FindFirst("UserId")?.Value ?? "System";

        /// <summary>
        /// 获取当前用户名
        /// </summary>
        public string Username => _httpContextAccessor.HttpContext?.User?.FindFirst("Username")?.Value ?? "System";

        /// <summary>
        /// 判断当前用户是否已认证
        /// </summary>
        public bool IsAuthenticated => _httpContextAccessor.HttpContext?.User?.Identity?.IsAuthenticated ?? false;
    }
} 